<?php
use oxide\plugin\PluginAbstract;
use oxide\http\Context;

define('FBSOCIAL_PLUGIN_DIR', dirname(__FILE__));

class SocialPlugin extends PluginAbstract
{
   private 
           $_comments_enabled = true,
           $_share_enabled = true,
           $_comment_exceptions = array();
   
   protected function onInit(Context $context) 
   {      
      $config = $this->getConfig();
      
      // getting the comment exception list
      $comment_exception_types = explode(',', $config->get('comment_exception_types', ''));
      array_walk($comment_exception_types, 'trim');
      $this->_comment_exceptions = $comment_exception_types;
      
      // check if comments are enabled
      $comments_enabled= $config->getValue('comments_enabled', true);
      $share_enabled = $config->getValue('share_enabled', true);
      $this->_comments_enabled = $comments_enabled;
      $this->_share_enabled = $share_enabled;
      
      $this->register('ModuleContentViewRenderStart');
      $this->register('ActionControllerViewRenderStart');
      $this->register('ModuleContentInfoRenderContentFooter');
      $this->register('ModuleContentViewContentRenderEnd');
   }
   
   /**
    * 
    * @param type $event
    * @param type $sender
    * @param \module\content\model\Content $content
    */
   public function onModuleContentViewRenderStart($event, $sender = null, \module\content\model\Content $content = null)
   {
      $config = $this->getConfig();
      
      $name_key = 'property';
      $type = 'blog';
      $site_name = $config->getValue('site_name');
      $admin_ids = $config->getValue('facebook_admin_ids');
      $url = _url::current();
      $image = null;
      _html::meta('og:type', $type, $name_key);
      _html::meta('og:site_name', $site_name, $name_key);
      _html::meta('og:admins', $admin_ids, $name_key);
      _html::meta('og:title', $content->title, $name_key);
      _html::meta('og:image', $image, $name_key);
      _html::meta('og:url', $url, $name_key);
   }
   
   /**
    * adding the script library when the view rendering starts
    * 
    * @param type $event
    * @param \oxide\application\ActionController $sender
    * @param type $arg
    * @return type
    */
   public function onActionControllerViewRenderStart($event, \oxide\application\ActionController $sender = null, $arg = null) 
   {
      if(!$this->_comments_enabled && !$this->_share_enabled) return;
      
      // we should only use if this is content module and for view action
      if(!($sender->getModuleName() == 'content' &&
           $sender->getActionName() == 'view')) {
         return;
      }
      
      // we also need to see if th
      
      $root = <<<'EOD'
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
EOD;
      echo $root;
   }
   
   /**
    * adding the like and share links at the end of the bottom term lists
    * 
    * @param type $event
    * @param \oxide\application\View $sender
    * @param type $arg
    */
   public function onModuleContentInfoRenderContentFooter($event, \oxide\application\View $sender = null, $arg = null)
   {      
      if(!$this->_share_enabled) return;
      if($sender->get('content')) { // need a better way to check if content
         $dl = $arg['terms'];
         $content = $arg['content'];
         $url = _url::current();
         $title = $content->title;
         $twitter = <<<EOF
 <a href="https://twitter.com/share" class="twitter-share-button" data-dnt="true">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
EOF;
         $googleplus = <<<EOF
<!-- Place this tag where you want the +1 button to render. -->
<div class="g-plusone" data-size="medium"></div>

<!-- Place this tag after the last +1 button tag. -->
<script type="text/javascript">
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>
EOF;
         $reddit = <<<EOF
<a href="http://www.reddit.com/submit" onclick="window.location = 'http://www.reddit.com/submit?url=' + encodeURIComponent(window.location); return false"> <img src="http://www.reddit.com/static/spreddit1.gif" alt="submit to reddit" border="0" /> </a>
EOF;
//         $dl['Be social'][] = '<a style="border:0px" target="_blank" href="https://www.facebook.com/sharer/sharer.php?s=100&p[url]='.$url .'&p[title]='.htmlspecialchars($title).'"><img src="/plugin/Social/resource/f_share.png" /></a>';
         $dl['Be social'][] = '<div class="fb-like" data-href="'.$url.'" data-send="false" data-layout="button_count" data-width="100" data-show-faces="false"></div>';
         $dl['Be social'][] = $twitter;
//         $dl['Be social'][] = $googleplus;
      }
   }
   
   /**
    * adding comment box at the end
    * @param type $event
    * @param type $sender
    * @param \module\content\model\Content $arg
    * @return type
    */
   public function onModuleContentViewContentRenderEnd($event, $sender = null, \module\content\model\Content $arg = null)
   {
      if(!$this->_comments_enabled) return;
      if(in_array($arg->content_type, $this->_comment_exceptions)) return;
      
      $url = _url::current();
      $output = <<<EOD
<section class='plugin-social'>
   <h3>Comments</h3>
<div class="fb-comments" data-href="$url" data-width="600" data-num-posts="10"></div>
</section>
EOD;
      echo $output;
   }
}
?>
